%Truncated regression model
function [beta]  = BetaTruncated(rStock,rIndex, limit)
    [Hn Hu Hd] = ClassifyLimit(rIndex, rStock, limit);
    
    [beta, ~, r] = regress(rStock, rIndex);
    sigma = var(r);
    theta0 = [beta sigma];
    lb = [beta,sigma];
    ub = [beta*1.1,1];
    %Definitions Options
%     options = optimset('Algorithm','interior-point');
%     theta = fmincon(@BetaTruncatedLikelihood, theta0,[], [], [], [], lb, ub, [], options, Hn(:, 1), Hn(:, 2), theta0, limit);
    
    %Definitions Options
    options = optimset('Display', 'iter');
    options = optimset(options,'LargeScale','off');
    options = optimset(options,'GradObj','on','Jacobian','off');
    options = optimset('Algorithm','active-set');

    theta = fminsearchbnd(@(x) -BetaTruncatedLikelihood(x, Hn(:, 1), Hn(:, 2), limit), theta0, lb, ub);
   
    beta = theta(1);
end